<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class clean_cache extends wsBackend
{
    var $dir = array('frontend_common', 'category_page', 'others', 'wallpaper_page', 'home','Wallpapers_page','Search_page','Resolutions_page','tags_page');

    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('directory', 'number'));
    }

    public function index()
    {
        $this->tpl->meta ['title'] = '清理缓存 - ' . $this->config->item('site_name');
        $this->tpl->meta ['description'] = '';
        $this->tpl->meta ['keywords'] = '';
        $this->tpl->page_header = '清理缓存';
        $this->tpl->page_header_ = '更新内容后必须清理缓存';
        $data = array();
        $cache_dir = dirname($this->db->cachedir);
        $cache_dir = rtrim($cache_dir, '/') . '/';
        $cache_folder = $this->input->get('cache_folder');
        foreach ($this->dir as $default_dir) {
            if (!is_dir($cache_dir . $default_dir)) {
                mkdir($cache_dir . $default_dir, 0777, true);
            }
        }
        $data['cache_folder'] = false;
        $data['a_link'] = true;
        if ($cache_folder) {
            $cache_folder = str_replace('/', '', $cache_folder) . '/';
            $cache_dir .= $cache_folder;
            $data['a_link'] = false;
            $data['cache_folder'] = $cache_folder;
        }
        if (!is_dir($cache_dir))
            redirect(admin_url('clean_cache'));
        $map = directory_map($cache_dir, 1);
        $data['dir_map'] = $map;
        $data['cache_dir'] = $cache_dir;
        $this->tpl->contents [] = $this->tpl->fetch('contents/cache_clean', $data);
        $this->tpl->render();
    }

    public function removedir()
    {
        $path = $this->input->get('dir');
        if (!$path)
            redirect(admin_url('clean_cache'));
        $path = preg_replace('/\s+/im', '+', $path);
        $this->load->helper('file');
        $cache_dir = dirname($this->db->cachedir);
        $cache_dir = rtrim($cache_dir, '/') . '/';
        $cache_dir .= $path;
        delete_files($cache_dir, true, 5);
        $this->session->set_flashdata('SUCCESS', '缓存清理成功');
        redirect(admin_url('clean_cache'));
    }

    public function removeall()
    {
        $dir = dirname($this->db->cachedir);
        $this->load->helper('file');
        foreach ($this->dir as $d) {
            delete_files($dir . '/' . $d, true);
        }
        redirect(admin_url('clean_cache'));
    }
}